//
//  PasswordViewController.m
//  seer
//
//  Created by Lin Eidan on 14-5-28.
//  Copyright (c) 2014年 haomee. All rights reserved.
//

#import "PasswordViewController.h"
#import "PersonalInfoViewController.h"
#import "Header.h"
#import "config.h"

@interface PasswordViewController ()

@property (strong, nonatomic) IBOutlet UIView *navBarView;
@property (strong, nonatomic) IBOutlet UITextField *passwordField;

@end

@implementation PasswordViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.isResetPassword = NO;
        self.isFromThirdLogin = NO;
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self drawNavTopContentView:self.navBarView];
    // Do any additional setup after loading the view from its nib.
}

#pragma -mark click

- (IBAction)submit:(id)sender {
    
    if (self.passwordField.text.length < 6) {
        [PopTips tipsAtTop:self.view Msg:NotifyInvalidPW];
        return;
    }
    
    NSString *ak = [Common md5:[NSString stringWithFormat:@"%@%@",self.phoneNum,[Common md5:self.passwordField.text]]];
    NSString *pw = [Common md5:self.passwordField.text];
    
    NSString *url = [NSString stringWithFormat:@"%@phoneReg&mobile=%@&pas=%@&ak=%@",API_BASE,self.phoneNum,pw,ak];
    
    if (self.isResetPassword || self.isFromThirdLogin) {
        url = [NSString stringWithFormat:@"%@resetPwd&mobile=%@&pas=%@&ak=%@",API_BASE,self.phoneNum,pw,ak];
    }
    
    [kApp displayWaitViewWithAllMaskView:NO];
    [[AFGetData sharedInstance] startGet:url andResultIsJSON:YES andRequestCompletion:^(id responseObj, BOOL isSuccess) {
        [kApp hideWaitView];
        if (isSuccess) {
            NSDictionary *dic = responseObj;
            int flag = [[dic objectForKey:@"flag"] intValue];
            if (flag == 1) {
                
                NSDictionary *user = [dic objectForKey:@"user"];
                
                if (user && !self.isResetPassword) {  //不是忘记密码的话，就是注册的时候
                    [UserObj sharedInstance].uid = [user objectForKey:@"uid"];
                    [UserObj sharedInstance].accessKey = [user objectForKey:@"accesskey"];
                    [UserObj sharedInstance].phoneNum = self.phoneNum;
                    [[UserObj sharedInstance] saveUserToLocal];
                    
                    PersonalInfoViewController *personalInfoVC = [[PersonalInfoViewController alloc] initWithNibName:@"PersonalInfoViewController" bundle:nil];
                    personalInfoVC.firstRegisterToGoTo = YES;
                    [self.navigationController pushViewController:personalInfoVC animated:YES];
                    
                }else{  //忘记密码，成功重设
                    [UserObj sharedInstance].isLogin = NO;
                    [PopTips tips:kApp.rootNav.view Msg:NotifyResetPwSuccess];
                    [self.navigationController popToViewController:kApp.meHomeVC animated:YES];
                }
                
                
            }else if (2 == flag){
                if (self.isResetPassword) {
                    [PopTips tipsAtTop:self.view Msg:NotifyResetPwFaild];
                    return ;
                }
                [PopTips tipsAtTop:self.view Msg:NotifyPhoneHasRegister];
            }else{
                if (self.isResetPassword) {
                    [PopTips tipsAtTop:self.view Msg:NotifyResetPwFaild];
                    return ;
                }
                [PopTips tipsAtTop:self.view Msg:NotifyRegisterFaild];
            }
        }else{
           [[kHandle sharedInstance] showTipsWhenRequestFailedWithView:self.view];
        }
    }];
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	[self.view.subviews enumerateObjectsUsingBlock:^(UIView* obj, NSUInteger idx, BOOL *stop) {
		if ([obj isKindOfClass:[UITextField class]]) {
			[obj resignFirstResponder];
		}
	}];
}


- (IBAction)goBack:(id)sender {
    [kApp hideWaitView];
    [[AFGetData sharedInstance] removeAllGetAndPostRequest];
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
